﻿#ifndef _RWLIST_H
#define _RWLIST_H

#include "threadlib.h"
#include <vector>

template<typename T>
class RWList
{
private:
    typedef vector<T>  VECTOR_TYPE;

public:
    RWList()
    {
        m_pWriteList = &m_listM;
        m_pReadList =  &m_listN;
    }

    void        pushElement(T pElement)
    {
        m_rwLock.lock();
        m_pWriteList->push_back(pElement);
        m_rwLock.unLock();
    }

    VECTOR_TYPE&  getReadList()
    {
        swapRW();

        return *m_pReadList;
    }

    size_t          currentWriteSize()
    {
        return m_pWriteList->size();
    }

private:
    void        swapRW()
    {
        // 交换读写队列指针
        m_rwLock.lock();
        VECTOR_TYPE* pTemp = m_pReadList;
        m_pReadList = m_pWriteList;
        m_pWriteList = pTemp;
        m_rwLock.unLock();
    }

private:
    // 读写队列指针
    VECTOR_TYPE*    m_pWriteList;
    VECTOR_TYPE*    m_pReadList;

private:
    ThreadLib::Mutex    m_rwLock;

    VECTOR_TYPE     m_listM;
    VECTOR_TYPE     m_listN;
};

#endif
